<template>
	<view class="content">
		<!-- <view class="live_banner">
			<view class="t">不上干货不直播</view>
			<view class="txt">破除一线二线城市技术信息不对称</view>
		</view> -->
		<view class="live_list m-t30">
			<swiper class="live-swiper" circular  autoplay="autoplay" indicator-dots="true" indicator-active-color="rgba(220,16,17,1)">
				<swiper-item v-for="(item ,index) in (liveList.slice(0,6))" :key="index" class="live-swiper-item" @click="navToDetailPage(item,'/pages/live/show')">
					<view class="swiper-h">
						<view class="title uni-ellipsis">{{item.title}}</view>
						<view class="time"><text v-if="item.beginTime">{{item.beginTime}}</text>{{item.subNum || 0}}人已预约</view>
						<view class="cl"></view>
						<view v-if="item.play_status == 2">
							<view class="btn">进入课堂</view>
						</view>
						<view v-else>
							<view class="btn">直播回看</view>
						</view>
						<view class="teacher">主讲：{{item.teacher}}</view>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="live_all m-t5">
			<view class="uni-list">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, index) in liveList"  :key="index" @click="navToDetailPage(item,'/pages/live/show')">
					<view class="uni-media-list">
						<view class="uni-media-list-logo">
							<image :src="item.logo" mode="aspectFill"></image>
						</view>
						<view class="uni-media-list-body">
							<view class="uni-media-list-text-top uni-ellipsis">{{item.title}}</view>
							<view class="uni-media-list-text-bottom uni-ellipsis" >{{item.course || '课程'}} · {{item.pay_type || '免费'}} · {{item.subNum || 0}}人报名</view>
							<view class="uni-media-list-text-bottom uni-ellipsis">主讲：{{item.teacher}}</view>
							<view v-if="item.play_status == 2">
								<view class="btn">进入课堂</view>
							</view>
							<view v-else>
								<view class="btn">直播回看</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				loadingType: 'more', 
				liveList1: [],
				liveList:[],
				page: 1,
			}
		},
		onLoad(options){
			// #ifdef H5
			this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight+'px';
			// #endif
			this.cateId = options.tid;
			this.loadData();
		},
		onPageScroll(e){
			//兼容iOS端下拉时顶部漂移
			if(e.scrollTop>=0){
				this.headerPosition = "fixed";
			}else{
				this.headerPosition = "absolute";
			}
		},
		//下拉刷新
		onPullDownRefresh(){
			this.page = 1;
			this.loadData('refresh');
		},
		//加载更多
		onReachBottom(){
			if(this.loadingType === 'nomore'){
				return;
			}
			if(this.loadingType === 'loading'){
				return;
			}
			this.page++;
			this.loadData('add');
		},
		methods: {
			cutText(item){
				if(item && item.description){
					return item.description.substring(0,25);
				}else{
					return '';
				}
			},
			//加载，带下拉刷新和上滑加载
			async loadData(type='add', loading) {
				/* let liveList1 = await this.$api.json('liveList');
				this.liveList1 = liveList1; */
				
				//没有更多直接返回
				if(type === 'add'){
					if(this.loadingType === 'nomore'){
						return;
					}
					this.loadingType = 'loading';
				}else{
					this.loadingType = 'more'
				}
				
				// let liveList = await this.$api.json('liveList');
				
				//直播课
				let params = {};
				params.page = this.page;
				params.pagesize = 10;
				await this.$http.post('api/live/livelist',params).then(res=>{
					if(res.code === this.sucCode){
						let liveList =  res.data.list || [];
						if(type === 'refresh'){
							this.liveList = [];
						}
						this.liveList = this.liveList.concat(liveList);
						
						//判断是否还有下一页，有是more  没有是nomore
						this.loadingType  = liveList.length < params.pagesize ? 'nomore' : 'more';
						if(type === 'refresh'){
							if(loading == 1){
								uni.hideLoading()
							}else{
								uni.stopPullDownRefresh();
							}
						}
					}else{
						this.$mHelper.toast(res.msg);
					}
					
				}).catch(err=>{
					console.log(err);
				});
				
				
			},
			//详情
			navToDetailPage(item,url) {
				uni.navigateTo({
					url: url + '?id='+item.id
				})
			},
		},
	}
</script>